<?php

namespace app\api\controller;

use app\common\controller\Api;
use EasyWeChat\Kernel\Messages\News;
use think\Db;
use think\Validate;
use app\api\model\UserAddress as AddressModel;
use app\api\model\User as UserModel;

class Address extends Api
{
    protected $noNeedLogin = [
        'Addresslist'
    ];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    public function addAddress()
    {
        $params = $this->request->param();
        $uid = $this->auth->id;
        if (!$uid) $this->success('请先进行登录再进行操作','',401);
        $UserModel = new UserModel();

        $user = $UserModel::get($uid);

        if (!$user) $this->success('该用户不存在,请从新登录','',401);

        $rules = [
            'name' => 'require|max:30',
            'phone' => 'require|number',
            'province'  => 'require|max:50',
            'coty'  => 'require|max:50',
            'address' => 'require'
        ];

        $message = [
            'name.require' => '收货人必须填写',
            'name.max' => '收货人姓名不能超出30',
            'phone.max' => '联系电话必须填写',
            'phone.number' => '请填写正确电话',
            'province.require' => '请填写省份',
            'province.max' => '省份名称不可超出50',
            'coty.require' => '请填写所在市区',
            'address.require' => '请填写详细地址',
        ];
        $Validate = new Validate($rules,$message);

        if (!$Validate->check($params))
        {
            $this->success($Validate->getError(),'',400);
        }
        $AddressModel = new AddressModel();


        $AddressModel->data([
            'uid' => $uid,
            'name' => $params['name'],
            'phone' => $params['phone'],
            'province' => $params['province'],
            'coty' => $params['coty'],
            'area' => $params['area'],
            'address' => $params['address'],
            'create_time' => time()
        ]);
        $AddressModel->save();
        $addressId = $AddressModel->id;
        if ($params['type'] == 1)
        {
            $user->address_id = $addressId;
            $user->save();
        }

        $this->success('添加成功',$addressId,200);
    }

    public function del()
    {
        $uid = $this->auth->id;
        $ids = $this->request->param('ids');
        if (!$ids) $this->success('选择您要操作的信息','',400);
        $UserModel = new UserModel();
        $user = $UserModel::get($uid);
        if (!$user) $this->success('用户不存在','',401);

        $AddressModel = new AddressModel();
        $AddressModel->del($ids,$user->id);
        $this->success('删除成功','',200);
    }

    /**
     * 设置默地址
     */
    public function default()
    {
        $uid = $this->auth->id;
        $addressId = $this->request->param('id');
        $type = $this->request->param('type');
        $UserModel = new UserModel();
        $user = $UserModel->getUser($uid);
        if (!$user) $this->success('该用户不存在,请从新登录','',401);

        if (!$addressId) $this->success('请选择您要操作的地址','',40);
        $AddressModel = new AddressModel();
        $address = $AddressModel->getAddress($addressId);
        if (!$address)$this->success('地址信息不存在,请从新添加','',400);


        if ($type == 1)
        {
            $UserModel->updateUser($uid,['address_id' => $addressId]);
            $this->success('设置成功','',200);
        }else{
            $UserModel->updateUser($uid,['address_id' => 0]);
            $this->success('已取消','',200);
        }

    }

    /**
     * 收货地址列表
     */
    public function Addresslist()
    {
        $uid = $this->auth->id;
        $user = new UserModel();
        $userinfo = $user->getUser($uid);
        if (!$userinfo) $this->success('用户信息不存在','',400);

        $AddressModel = new AddressModel($uid);
        $addresslist = $AddressModel->getaddresslist($uid);

        if ($addresslist)
        {
            $this->success('查询成功',$addresslist,200);
        }else{
            $this->success('没有添加地址','',200);

        }
    }

    /**
     * 编辑地址
     */
    public function update()
    {
        $uid = $this->auth->id;
        $id    = $this->request->param('id');
        $name  = $this->request->param('name');
        $phone = $this->request->param('phone');
        $province = $this->request->param('province');
        $city  = $this->request->param('city');
        $area  = $this->request->param('area','');
        $address = $this->request->param('address');
        $type = $this->request->param('type',0);

        if (empty($id) || empty($name) || empty($phone) || empty($province) || empty($city) || empty($address))
        {
            $this->success('缺少参数', '', 400);
        }

        $addressRes = AddressModel::get($id);

        if (!$addressRes)
        {
            $this->success('地址信息不存在', '', 400);
        }

        if ($addressRes['uid'] != $uid)
        {
            $this->success('您无法操作','',400);
        }

        $addressRes->name     = $name;
        $addressRes->phone    = $phone;
        $addressRes->province = $province;
        $addressRes->coty     = $city;
        $addressRes->area     = $area;
        $addressRes->address  = $address;
        $addressRes->save();

        if ($type == 1)
        {
            UserModel::where('id',$uid)->update([
                'address_id' => $id
            ]);
        }

        $this->success('更新成功', $addressRes->id, 200);

    }

}
